'use strict';

angular.module('app')
    .controller('launchPlanDetailController', [ '$rootScope', '$scope', '$http', '$state',
        function($rootScope, $scope, $http, $state) {
            $scope.title = '投放计划详情';
            $scope.loading = false;
            var adPlanId = $state.params.adPlanId;
            $scope.param={};
            //详情初始化
            activate(adPlanId);
            //计划下广告初始化
            adSlots(adPlanId)

            // 初始化页面
            function activate(adPlanId) {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name+'/launchPlan/detail',
                    data: {'adPlanId': adPlanId}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.record = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }


            // 获取投放计划下的广告
            function adSlots(adPlanId) {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name+'/launchPlan/adSlots',
                    data: {'adPlanId': adPlanId}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        console.log(result.data);
                        $scope.adSlots = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.approve = function(id, state) {
                $scope.alertMessage({
                    text:"确定做此操作吗？", //内容
                    btnCancel:true, //取消按钮
                    btnConfirm:true //确认按钮
                }).then(function () {

                    if(state==3){
                        $.ajax({
                            url : $scope.app.project_name + '/launchPlan/approve',
                            data: {'id': id, 'state' : state}
                        }).then(function(result) {
                            $scope.loading = false;
                            if (result.httpCode == 200) {
                                //详情初始化
                                activate(adPlanId);
                                //计划下广告初始化
                                adSlots(adPlanId);
                                $scope.alertModal({"content":"操作成功"});
                            } else {
                                $scope.alertModal({"content":"操作失败"});
                            }
                        });
                    }else{
                        $scope.param = {};
                        $scope.param.title ="推广计划";
                        $("#title").text($scope.param.title);
                        $scope.param.id= id;
                        $scope.param.type = "PL";
                        $('#refuseUserSupply').modal('show', true);
                    }

                })
            }


            $scope.approveAd = function(id, state) {
                    $scope.alertMessage({
                        text:"确定做此操作吗？", //内容
                        btnCancel:true, //取消按钮
                        btnConfirm:true //确认按钮
                    }).then(function () {
                        if(state==3){
                            $.ajax({
                                url : $scope.app.project_name + '/launchPlan/approveAd',
                                data: {'id': id, 'state' : state}
                            }).then(function(result) {
                                $scope.loading = false;
                                if (result.httpCode == 200) {
                                    // //详情初始化
                                    // activate(adPlanId);
                                    //计划下广告初始化
                                    adSlots(adPlanId);
                                    $scope.alertModal({"content":"操作成功"});
                                } else {
                                    $scope.alertModal({"content":"操作失败"});
                                }
                            });
                        }else {
                            $scope.param = {};
                            $scope.param.title ="广告";
                            $("#title").text($scope.param.title);
                            $scope.param.id= id;
                            $scope.param.type = "AD"
                            $('#refuseUserSupply').modal('show', true);
                        }
                    })
            }

            $scope.saveApproveReason = function () {
                var reason = $scope.param.approveReason;
                if(reason==''||reason==null){
                    $scope.alertModal({"content":"请输入拒绝原因"});
                    $('#refuseUserSupply').modal('hide');
                    return;
                }
                var url = $scope.param.type=="AD"?"approveAd":"approve";
                $.ajax({
                    url : $scope.app.project_name + '/launchPlan/'+url,
                    data: {'id':$scope.param.id,'state':4,'reason':reason}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        // //详情初始化
                        activate(adPlanId);
                        //计划下广告初始化
                        adSlots(adPlanId);
                        $scope.alertModal({"content":"操作成功"});
                    } else {
                        $scope.alertModal({"content":"操作失败"});
                    }
                });
            }

            $scope.addPosArr={
                "pc":[0,1,2,3,4,5,6,7,8],
                "mobile":[0,1,2]
            }

            $scope.showPosWrap=function (id) {
                $("#PosWrap"+id).show();
                $("#PosWrap"+id).click(function () {
                    $("#PosWrap"+id).hide();
                })
            }
        } ]);